/*
 * ButtonView.cpp
 *
 *   This is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2 of the License, or (at your option) any later version.
 *
 *   This software is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *  Created on: 2010-1-3
 *      Author: Huang Haiyang
 */

#include "../ButtonView.h"
#include "../../resource/ImageCache.h"
#include "../OnClickListener.h"

namespace view
	{

	CButtonView::CButtonView()
		{		
		}

	CButtonView::~CButtonView()
		{
		}

	void CButtonView::SetImageSrc(const TDesC& aSrc)
		{
		iSrcImage.Copy(aSrc);
		if (IsReadyToDraw())
			{
			DrawNow();
			}
		}
	
	TSize CButtonView::Measure(const TSize& aSize)
		{
		TSize needSize = resource::CImageCache::getInstance()->GetImageSize(iSrcImage);
		if (needSize.iWidth > aSize.iWidth)
			needSize.iWidth = aSize.iWidth;
		if (needSize.iHeight > aSize.iHeight)
			needSize.iHeight = aSize.iHeight;
		
		return needSize;
		}
	
	void CButtonView::SetOnClickListener(OnClickListener* aListener)
		{
		iListener = aListener;
		}
	
	void CButtonView::SetMapKey(TUint aKey)
		{
		iMapKey = aKey;
		}
	
	void CButtonView::ConstructL(const TRect& aRect, CCoeControl* aParent)
		{
		SetContainerWindowL(*aParent);
		SetRect(aRect);
		ActivateL();
		}
	
	TKeyResponse CButtonView::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
		{
		if (aType == EEventKey && aKeyEvent.iCode == iMapKey)
			{
			if (iListener)
				{
				AnimateClick();
				iListener->OnClick(this);
				}
			return EKeyWasConsumed;
			}
		return CCoeControl::OfferKeyEventL(aKeyEvent, aType);
		}
			
	void CButtonView::Draw(const TRect& aRect) const
		{
		TSize szImage;
		TPoint pt;
		CWindowGc& gc = SystemGc();
		data::CPNG* image = resource::CImageCache::getInstance()->GetImage(iSrcImage);
		if (!image || !image->iRawFore)
			{
			return;
			}
		
		szImage = image->iRawFore->SizeInPixels();
			
		pt.iX = Rect().iTl.iX + (Rect().Width() - szImage.iWidth) / 2;
		pt.iY = Rect().iBr.iY - szImage.iHeight;
		
		if (image->iRawMask)
			{
			gc.BitBltMasked(pt, image->iRawFore, TRect(szImage), image->iRawMask, ETrue);
			}
		else
			{
			gc.BitBlt(pt, image->iRawFore);
			}
		
		if (iClicking) 
			{
			DrawLight(gc, pt);
			}
		}
	
	void CButtonView::DrawLight(CWindowGc& aGc, const TPoint& aPt) const
		{
		TSize szImage;
		TPoint pt;
		data::CPNG* image = resource::CImageCache::getInstance()->GetImage(_L("res\\light.png"));
		if (!image || !image->iRawFore)
			{
			return;
			}
		
		szImage = image->iRawFore->SizeInPixels();
		pt.iX = aPt.iX - (szImage.iWidth / 2);
		pt.iY = aPt.iY;
		
		aGc.BitBltMasked(pt, image->iRawFore, TRect(szImage), image->iRawMask, ETrue);	
		}

	void CButtonView::AnimateClick()
		{
//		iClicking = ETrue;
//		DrawNow();
//		iClicking = EFalse;
//		DrawNow();
		}
	
	}
